if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu"); 
 
function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!");
    while (true) {
      var allow = text("允许").findOne(5000);
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!");
        allow.click();
        sleep(1000);
        thread.interrupt();
        break;
      }
    }
  });
  sleep(1000);
  var result = launchApp(app_name);
  sleep(3000);
  thread.interrupt();
  return result;
}

function task(){
  //跳过闪屏页广告
  var jump = className('android.widget.TextView').clickable().depth(6).indexInParent("1").drawingOrder(2).findOne(2000)
  if(jump){
    jump.click();
    sleep(2000);
  }  
  utils.updateAppProgress(10);
  var cnt  = 0;
  while(cnt < 6){
    var 任务= id("qx.com.cn.qxideo:id/gifIv").findOne(1000);
    if(任务){
        var finalActivity = currentActivity();
        任务.click();
        toastLog("第 "+ cnt +" 个视频广告中..")        
        sleep(5000);
        utils.waitAdvert(finalActivity);
        sleep(3000)
    }
    cnt ++;
    utils.updateAppProgress(10 * cnt );
  }
  toastLog("观看视频任务完成") 
  var 任务结算 = text("任务结算").findOne(3000);
  if(任务结算){
    任务结算.click();
    var 一键结算 = text("一键结算").findOne(3000);
    if(一键结算){
      sleep(500);
      var finalActivity = currentActivity();
      一键结算.click();
      sleep(5000);
      utils.waitAdvert(finalActivity); 
      toastLog("福音任务完成")
    }else{
      toastLog("没有找到一键结算按钮")
    }
  }else{
    toastLog("没有找到结算任务")
  }
}


function main() {
  let open_app = launchAppEnhance("福音短视频");
  if(open_app) {
    task()
  }
  else {
    toastLog("福音短视频开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}

storage.put("script_running_status", "end");
